home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Freeware / Read It Later 0.9924 / read_it_later-0.9924-fx.xpi / chrome / isreaditlater.jar / content / ISRILinit.js < prev    next >
Text File  |  2008-10-30  |  11KB  |  333 lines

  1. // --- Master Init Functions --- //
  2.  
  3. function ISRIL_init() {
  4.     
  5.     
  6. }
  7.  
  8. ISRIL_init.prototype = {    
  9.  
  10.     _init : function() {
  11.         
  12.         if (!ISRIL.inited) {
  13.         
  14.             try {  
  15.                     
  16.                 // -- Soft Init -- Items with little chance of failing but useful for displaying error messages nicely -- //
  17.                 ISRILxul._init();
  18.                 
  19.                 // -- Get Globals
  20.                 Components.utils.import("resource://rilglobals/ISRILglobals.js");
  21.                 
  22.                 
  23.                 // -- Connect to Major Firefox services -- Normally the point failure for startup errors -- //
  24.                 ISRIL_init.startServices();
  25.                 ISRIL.Connect(); //Connect to Database
  26.                 
  27.                 ISRIL_init.loadInVars();
  28.                 
  29.                 ISRIL_init.addToToolbar();    
  30.                 
  31.  
  32.                 // --- Init Other Objects --- //
  33.                 ISRILxul._init_finish();
  34.                 ISRILpr._init();
  35.                 ISRILsync._init();
  36.                 ISRILoffline._init();
  37.         
  38.                 
  39.                 ISRIL_init.installUpgrade();
  40.                 
  41.                 ISRIL_init.preloadCaches();
  42.                 
  43.                 ISRIL_init.eventsObservers();
  44.                 
  45.                 ISRIL_init.verify();
  46.                 
  47.                 ISRIL_init.wrapUp();
  48.                 
  49.             } catch(err) {
  50.                 try {
  51.                     var msg = err.fileName + ' | ' + err.lineNumber + "\n" + err;
  52.  
  53.                     //Try to determine if it's a common error
  54.                     var errorArgs = {
  55.                         txt:        ISRIL.l('error_startup'),
  56.                         txt2:        msg,
  57.                         buttonTxt:    ISRIL.l('GetHelp'),
  58.                         hideAll:     true,
  59.                         noCancel:    true,
  60.                         onclk    :    "ISRILxul.recentWindow().getBrowser().selectedTab = ISRILxul.recentWindow().getBrowser().addTab('http://www.ideashower.com/uncategorized/problem-starting-read-it-later/')",
  61.                     };
  62.                     
  63.                     if (err.message.match('nsIJSCID.getService')) {
  64.                         /*
  65.                         [Exception... "Component returned failure code: 0x80570016 (NS_ERROR_XPC_GS_RETURNED_FAILURE) [nsIJSCID.getService]ö nsresult: ô0╫80570016 (NS_ERROR_XPC_GS_RETURNED_FAILURE)ö location: ôJS frame :: chrome://isreaditlater/content/ISRIL.js :: anonymous :: line 26? data: no]
  66.                         */
  67.                         //Can't connect to bookmark service
  68.                         //Corrupt bookmarks or Norton 360 Conflicts
  69.                         errorArgs.txt         =    ISRIL.l('ErrorAccessBookmarks');
  70.                         errorArgs.txt2         =    ISRIL.l('ErrorCommonProblem');
  71.                         errorArgs.onclk        =    "ISRILxul.recentWindow().getBrowser().selectedTab = ISRILxul.recentWindow().getBrowser().addTab('http://www.ideashower.com/uncategorized/read-it-later-cant-access-bookmarks/')";
  72.                         
  73.                     } else if (err.message.match('nsINavHistoryService.executeQuery')) {
  74.                         /*
  75.                          [Exception... "Component returned failure code: 0x8000ffff (NS_ERROR_UNEXPECTED)
  76.                         [nsINavHistoryService.executeQuery]ö nsresult: ô0╫8000ffff
  77.                         (NS_ERROR_UNEXPECTED)ö location: ôJS frame ::
  78.                         chrome://isreaditlater/content/ISRIL.js :: anonymous :: line 366? data: no]
  79.                         */
  80.                         //Corrupted bookmarks file or changed GUID of RIL folder?
  81.                         errorArgs.txt         =    ISRIL.l('ErrorAccessFolder');
  82.                         errorArgs.txt2         =    ISRIL.l('ErrorCommonProblem');
  83.                         errorArgs.onclk        =    "ISRILxul.recentWindow().getBrowser().selectedTab = ISRILxul.recentWindow().getBrowser().addTab('http://www.ideashower.com/uncategorized/read-it-later-folder-issues/')";
  84.                     }
  85.                     ISRILxul.ListErrorSet(true, errorArgs);
  86.                     
  87.                     ISRIL_init.startupError = true;
  88.                     
  89.                 } catch(err2) {
  90.                     
  91.                     //Major failure, couldn't even display nice popups, so we'll have to do the harsh popup, yikes.
  92.                     alert( ISRIL.l('error_startup') + "\n\n" + err + "\n\n" + err2 );
  93.                     ISRIL.d('Startup Error: ' + "\n\n" + err + "\n\n" + err2);
  94.                     
  95.                 }
  96.             }
  97.         }
  98.         
  99.     },
  100.     
  101.     startServices : function() {
  102.         
  103.         Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  104.         
  105.         ISRIL.sPrompts = Components
  106.             .classes["@mozilla.org/embedcomp/prompt-service;1"]
  107.             .getService(Components.interfaces.nsIPromptService);
  108.             
  109.         ISRIL.sHistory = Components
  110.             .classes["@mozilla.org/browser/nav-history-service;1"]
  111.             .getService(Components.interfaces.nsINavHistoryService);
  112.             
  113.         ISRIL.sNotes = Components.classes["@mozilla.org/browser/annotation-service;1"]
  114.                         .getService(Components.interfaces.nsIAnnotationService);
  115.             
  116.         ISRIL.sUri = Components
  117.             .classes["@mozilla.org/network/io-service;1"]
  118.             .getService(Components.interfaces.nsIIOService);
  119.             
  120.         ISRIL.sTags = Components
  121.             .classes["@mozilla.org/browser/tagging-service;1"]
  122.             .getService(Components.interfaces.nsITaggingService);
  123.             
  124.         ISRIL.sIcon = Components
  125.             .classes["@mozilla.org/browser/favicon-service;1"]
  126.             .getService(Components.interfaces.nsIFaviconService);        
  127.         
  128.         ISRIL.ios = Components
  129.             .classes["@mozilla.org/network/io-service;1"]
  130.             .getService(Components.interfaces.nsIIOService);
  131.             
  132.         ISRIL.json = Components.classes["@mozilla.org/dom/json;1"]
  133.             .createInstance(Components.interfaces.nsIJSON);
  134.         
  135.         ISRIL.sDB = Components.classes["@mozilla.org/storage/service;1"]
  136.             .getService(Components.interfaces.mozIStorageService);
  137.         
  138.         ISRIL.sOb = Components.classes["@mozilla.org/observer-service;1"]
  139.             .getService(Components.interfaces.nsIObserverService);
  140.         
  141.         ISRIL.sBookmarks = Components
  142.             .classes["@mozilla.org/browser/nav-bookmarks-service;1"]
  143.             .getService(Components.interfaces.nsINavBookmarksService);
  144.         
  145.     },
  146.     
  147.     loadInVars : function() {        
  148.           
  149.         // --- Preferances --- //
  150.         ISRILprefs.loadDefaults();
  151.         
  152.  
  153.         // --- Get Read It Later Folder --- //
  154.         ISRIL.loadFolder();
  155.     
  156.     },
  157.     
  158.     addToToolbar : function() {
  159.  
  160.         // --- Add Buttons if not on UI --- //            
  161.         if (!ISRILprefs.prefB('added_to_toolbar') && !ISRILxul.bip('ReadSomething')) {
  162.             var firefoxnav = document.getElementById("nav-bar");
  163.             var curSet = firefoxnav.currentSet;
  164.             if (curSet.indexOf("my-extension-button") == -1) {
  165.                 var set;
  166.                 set = firefoxnav.currentSet + ",isRitL-ReadSomething";
  167.                 firefoxnav.setAttribute("currentset", set);
  168.                 firefoxnav.currentSet = set;
  169.                 document.persist("nav-bar", "currentset");
  170.                 BrowserToolboxCustomizeDone(true);
  171.             }
  172.             ISRILprefs.setPref('added_to_toolbar', true); //Only tried the first time, otherwise it does it once after they remove it
  173.         }    
  174.         
  175.     },
  176.     
  177.     installUpgrade : function() {
  178.         
  179.         // -- If First Run -- //
  180.         t = true;
  181.         if (!ISRILprefs.prefB('installed') || t) {
  182.             
  183.             //Check screen resolution to make sure default list size fits
  184.             var maxRows = ISRILxul.maxPageN();
  185.             if (maxRows < ISRILprefs.pref('list-page')) {
  186.                 ISRILprefs.setPref('list-page', maxRows);
  187.             }
  188.             
  189.             //Set this version as original install point
  190.             ISRILprefs.setPref('install-version', ISRIL.v);
  191.             
  192.             //Set as installed
  193.             ISRILprefs.setPref('installed', true);
  194.         }
  195.         
  196.         ISRILsync.GetAFeed();
  197.         
  198.         // --- Check Version and show Changelog on update --- //
  199.         if (ISRILprefs.pref('version') != ISRIL.v) {
  200.             window.addEventListener("load", function() { ISRIL.GoTo(ISRIL.cl, false, 'tab') }, false);
  201.             ISRILprefs.setPref('version', ISRIL.v);
  202.         }
  203.         
  204.     },
  205.     
  206.     preloadCaches : function() {
  207.         
  208.         ISRIL.GetList();
  209.         
  210.     },
  211.     
  212.     eventsObservers : function() {        
  213.         ISRIL_init.registerObserver('quit-application-requested');        
  214.         gBrowser.addProgressListener(ISRIL_init.urlBarListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_ALL);
  215.         ISRIL.sBookmarks.addObserver(ISRIL_init.bookmarkObserver, false);
  216.         
  217.         ISRIL.SetupKeyStrokes();
  218.  
  219.         document.getElementById('sidebar').addEventListener("unload", ISRILxul.SidebarEvent, true);
  220.     },
  221.     
  222.     verify : function() {        
  223.         // --- Check that both checkmarks are not appearing --- //
  224.         //if (!ISRILxul.bip('later') || !ISRILxul.bip('mark1')) {
  225.         //    throw ISRIL.l('error_startup');    
  226.         //}
  227.         
  228.     },
  229.     
  230.     wrapUp : function() {        
  231.         
  232.         ISRILglobals.mainWindow = window;
  233.         ISRIL.inited = true;
  234.         ISRIL.debug = ISRILprefs.prefB('debug');
  235.         ISRILglobals.inited = true;
  236.         ISRIL.checkPage();
  237.         
  238.     },    
  239.     
  240.     
  241.     // --  Setup Observers -- //
  242.  
  243.     urlBarListener : {
  244.         QueryInterface: function(aIID) {
  245.             if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
  246.                aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
  247.                aIID.equals(Components.interfaces.nsISupports))
  248.             return this;
  249.             throw Components.results.NS_NOINTERFACE;
  250.         },
  251.         
  252.         onLocationChange: function(aProgress, aRequest, aURI) {
  253.             ISRIL.checkPage(aURI);
  254.         },    
  255.         
  256.         onStateChange: function() {},    
  257.         onStatusChange: function() {},
  258.         onProgressChange: function() {},
  259.         onSecurityChange: function() {},
  260.         onLinkIconAvailable: function() {},
  261.     },
  262.     
  263.     bookmarkObserver : {
  264.         QueryInterface: function(aIID) { 
  265.             if (aIID.equals(Ci.nsINavBookmarkObserver) ||
  266.                 aIID.equals(Ci.nsISupports))
  267.             return this;
  268.             throw Cr.NS_NOINTERFACE;
  269.         },
  270.     
  271.         onBeginUpdateBatch: function() { },
  272.         onEndUpdateBatch: function() { },
  273.         onItemVisited: function() { },
  274.             
  275.         onItemAdded : function(aItemId, aFolder, aIndex) {
  276.             if (aFolder == ISRILglobals.folderId && !ISRILglobals.adding && aItemId != ISRILglobals.noSyncId) {
  277.                 ISRILsync.QueueNew( aItemId );
  278.             }
  279.         },
  280.         onItemRemoved: function(aItemId, aFolder, aIndex) {
  281.             if (aFolder == ISRILglobals.folderId && ISRILglobals.ListCache[aItemId] && aItemId != ISRILglobals.noSyncId) {
  282.                 ISRILsync.QueueUpdate( aItemId , 'read' );
  283.                 if (ISRILglobals.ListCache[aItemId] == ISRIL.url()) {
  284.                     ISRIL.checkPage();
  285.                 }
  286.             }
  287.         },
  288.         onItemMoved: function(aItemId, aOldParent, aOldIndex, aNewParent, aNewIndex) {
  289.             if (aOldParent == ISRILglobals.folderId && aNewParent != ISRILglobals.folderId && aItemId != ISRILglobals.noSyncId) {
  290.                 ISRILsync.QueueUpdate( aItemId , 'read' );    
  291.             } else if (aNewParent == ISRILglobals.folderId && aItemId != ISRILglobals.noSyncId) {
  292.                 ISRILsync.QueueNew( aItemId );    
  293.             }
  294.         },
  295.         onItemChanged: function(aItemId, aProperty, aIsAnnotationProperty) {
  296.             f = PlacesUtils.bookmarks.getFolderIdForItem( aItemId );
  297.             if (f == ISRILglobals.folderId && aItemId != ISRILglobals.noSyncId) {
  298.                 if (aProperty == 'tags') {
  299.                     ISRILsync.QueueUpdateTags( aItemId );
  300.                 } else if (aProperty == 'title') {
  301.                     ISRILsync.QueueUpdate( aItemId , 'update_title' );
  302.                 }
  303.             }
  304.         }
  305.     },
  306.     
  307.     
  308.     registerObserver : function(topic) {
  309.         ISRIL.sOb.addObserver(ISRIL, topic, false);
  310.     },
  311.     unregisterObserver : function(topic) {
  312.         ISRIL.sOb.removeObserver(ISRIL, topic);
  313.     },
  314.     
  315.     
  316.     // -- Unload -- //
  317.     
  318.     _uninit : function() {
  319.         gBrowser.removeProgressListener( ISRIL_init.urlBarListener ); //remove?
  320.         ISRIL.sBookmarks.removeObserver( ISRIL_init.bookmarkObserver );
  321.     },
  322.     
  323.     
  324. }
  325.  
  326.  
  327. ISRIL_init = new ISRIL_init();
  328.  
  329.  
  330. // --- Onload Events --- //
  331.  
  332. window.addEventListener("load", function() {ISRIL_init._init();}, false);
  333. window.addEventListener("unload", function() {ISRIL_init._uninit();}, false);